import { getTopPlaylist, getTopSinger } from '@/api/rank/index'
import { timeTransformData1, transformCount } from '@/utils/util'

export const transTopSinger = (res) => {
  const { data: { list: { artists, updateTime } } } = res
  artists.splice(5)
  const artistsList = artists.map(item => {
    const { id, name, picUrl } = item
    return {
      id,
      name,
      picUrl
    }
  })
  return {
    artistsList,
    backgroundImage: artistsList[0].picUrl,
    updateTime: timeTransformData1(updateTime)
  }
}

export const transTopPlaylist = (res) => {
  const { data: { playlist } } = res
  const { id, name, coverImgUrl, updateTime, tracks } = playlist
  const backgroundImage = tracks.length ? tracks[0].al.picUrl : ''
  if (tracks.length > 5) {
    tracks.splice(5)
  }
  const tk = tracks.map(item => {
    const { id, name, ar, alia } = item
    const aliaStr = alia.length ? '(' + alia.join('|') + ')' : ''
    const artists = ar.map(item => ({
      id: item.id,
      name: item.name
    }))
    return {
      id,
      name,
      artists,
      aliaStr
    }
  })
  return {
    id,
    name,
    coverImgUrl,
    backgroundImage,
    tk,
    updateTime: timeTransformData1(updateTime)
  }
}

export const transToplist = async (res) => {
  const { data: { list, artistToplist: { coverUrl } } } = res
  const official = []
  const global = []
  const singerObj = await getTopSinger()
  const singerTop = Object.assign(singerObj, { coverImgUrl: coverUrl })
  for (let i = 0; i < list.length; i++) {
    const { id, name, playCount, updateTime, coverImgUrl } = list[i]
    if (i < 4) {
      const officialItem = await getTopPlaylist(id)
      official.push(Object.assign(officialItem, { coverImgUrl }))
    } else {
      global.push({ id, name, playCount: transformCount(playCount), coverImgUrl })
    }
  }
  return {
    official,
    global,
    singerTop
  }
}
